package cn.tinlan.shopcoin.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import cn.tinlan.shopcoin.R;

/**
 * Created by Mortimer on 2018/11/20. 14:38
 * 佛祖🙏，永无bug。
 *
 */
public abstract class BaseFragment extends LazyLoadFragment {

    private View rootView;
//    protected StateView mStateView;//用于显示加载中、网络异常，空布局、内容布局
    protected Activity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(provideContentViewId(),container,false);
            ButterKnife.bind(this, rootView);

//            mStateView = StateView.inject(getStateViewRoot());
//            if (mStateView != null){
//                mStateView.setLoadingResource(R.layout.page_loading);
//                mStateView.setRetryResource(R.layout.page_net_error);
//            }

            initView(rootView);
            initData();
            initListener();
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    /**
     * 初始化一些view
     * @param rootView
     */
    public void initView(View rootView) {
    }

    /**
     * 初始化数据
     */
    public void initData() {

    }


    /**
     * 设置listener的操作
     */
    public void initListener() {

    }
    @Override
    protected void onFragmentFirstVisible() {
        //当第一次可见的时候，加载数据
        loadData();
    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();
    //加载数据
    protected abstract void loadData();

    @Override
    public void onDestroy() {
        super.onDestroy();

        rootView = null;
    }
}
